﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCamera : MonoBehaviour
{
    public Vector3 camInitPos = Vector3.zero; //相机初始化位置
    protected Vector3 InitPos, InitRot; //初始化位置&旋转

    //人物移动变量
    protected float currentMoveSpeed; //当前移动速度
    public float walkSpeed = 50; //移动速度
    public float runSpeed = 120; //移动加速

    //控制摄像机旋转的成员变量
    public float mouseSpeed = 5; //鼠标灵敏度
    [Range(0, 10)] public float damper = 10; //平滑度
    protected Vector2 targetAngles; //存储鼠标X，Y的值
    public Vector2 currentAngles { protected set; get; } //存储相机要旋转的值

    //控制摄像机移动的成员变量 
    Vector3 kNewPos; //移动位置
    float fMoveDeltaX; //X轴移动量
    float fMoveDeltaY; //Y轴移动量
    float fMoveDeltaZ; //Z轴移动量

    public Transform CamTrans;

    #region 初始化参数

    public virtual void InitParameter()
    {
        //初始化移动旋转
        InitPos = transform.position;
        InitRot = transform.eulerAngles;
        currentAngles = targetAngles = InitRot;

        SetCamPos();
    }

    #endregion

    #region 当前移动速度

    protected void CurrentMoveSpeed()
    {
        bool isWalk = !Input.GetKey(KeyCode.LeftShift);
        currentMoveSpeed = isWalk ? walkSpeed : runSpeed;
    }

    #endregion

    #region A、S、W、D 控制移动

    protected virtual void ControlMove()
    {
        //Debug.Log(1111);
        fMoveDeltaX = 0;
        fMoveDeltaY = 0;
        fMoveDeltaZ = 0;

        if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
        {
            fMoveDeltaX += Input.GetAxis("Horizontal") * currentMoveSpeed * Time.deltaTime;
            fMoveDeltaZ += Input.GetAxis("Vertical") * currentMoveSpeed * Time.deltaTime;
        }

        if (fMoveDeltaX != 0.0f || fMoveDeltaY != 0.0f || fMoveDeltaZ != 0.0f)
        {
            kNewPos = transform.position;
            kNewPos += transform.forward * fMoveDeltaZ;
            kNewPos += transform.right * fMoveDeltaX;
            kNewPos += transform.up * fMoveDeltaY;
            transform.position = kNewPos;
        }
    }

    #endregion

    #region 鼠标右键控制旋转

    protected virtual void ControlRotate()
    {
        if (Input.GetMouseButton(1))
        {
            Cursor.visible = false; //隐藏指针
            targetAngles.y += Input.GetAxis("Mouse X") * mouseSpeed;
            targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSpeed;

            currentAngles = Vector2.Lerp(currentAngles, targetAngles, damper * Time.deltaTime);
            transform.rotation = Quaternion.Euler(currentAngles);
        }
        else
        {
            Cursor.visible = true; //显示指针
        }

        //鼠标抬起初始化旋转值
        if (Input.GetMouseButtonUp(1))
            ResetCamera();
    }

    #endregion

    #region 视角升高降低

    protected virtual void ViewUpOrDown()
    {
        //视角升高
        if (Input.GetKey(KeyCode.E))
        {
            transform.position = Vector3.MoveTowards(transform.position, transform.position + Vector3.up,
                currentMoveSpeed * Time.deltaTime);
        }

        //视角降低
        if (Input.GetKey(KeyCode.Q))
        {
            transform.position = Vector3.MoveTowards(transform.position, transform.position + Vector3.down,
                currentMoveSpeed * Time.deltaTime);
        }
    }

    #endregion

    #region 滚轮视角控制

    protected virtual void ControlScrollWheel()
    {
        //视角前进&后退
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            transform.Translate(new Vector3(0, 0, 1));
            //位置限定
            kNewPos = transform.position;
            transform.position = kNewPos;

        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            transform.Translate(new Vector3(0, 0, -1));
            //位置限定
            kNewPos = transform.position;
            transform.position = kNewPos;
        }
    }

    #endregion

    #region 视角复位

    public void ViewRecover()
    {
        transform.position = InitPos;
        transform.rotation = Quaternion.Euler(InitRot);
        ResetCamera();
    }

    #endregion

    #region 修改相机父物体及位置

    public virtual void SetCamPos()
    {
        CamTrans.SetParent(transform);
        CamTrans.localPosition = camInitPos;
        CamTrans.localEulerAngles = Vector3.zero;
    }

    #endregion

    private void Update()
    {
        CamTrans.localPosition = camInitPos;
    }

    #region 相机位置旋转刷新

    public virtual void ResetCamera()
    {
        currentAngles = targetAngles = transform.eulerAngles;
    }

    #endregion
}
